package com.dpteam.service.impl;

import com.dpteam.bean.UserInfo;
import com.dpteam.dao.UserDao;
import com.dpteam.model.User;
import com.dpteam.service.UserService;
import com.dpteam.util.Constant;
import com.dpteam.util.TransformUtil;
import org.apache.commons.io.FileUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * User: luolei
 * Date: 2016/1/11
 * Time: 8:43
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    private static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Override
    public UserInfo login(String account, String password) throws UnknownAccountException, IncorrectCredentialsException{
        logger.info("login");
        //TODO 输入参数校验
        User user = userDao.findByAccount(account);
        if (user == null) {
            logger.info("用户名:[{}] 不存在", account);
            throw new UnknownAccountException("用户名错误");
        }
        if (!password.equals(user.getPassword())) {
            logger.info("用户名:[{}] 对应的密码:[{}]错误", account, password);
            throw new IncorrectCredentialsException("密码错误");
        }
        logger.info("登陆成功");
        UserInfo userInfo = TransformUtil.fromUser(user);
        return userInfo;
    }

    @Override
    public boolean logout() {
        logger.info("logout");
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return true;
    }

    @Override
    public UserInfo updateUser(UserInfo updateInfo) {
        //TODO 参数校验
        logger.info("updateUser");
        return null;
    }

    @Override
    public boolean updatePass(String oldPassword, String newPassword) {
        //TODO 参数校验
        logger.info("updatePass([oldPassword:{}], [newPassword:{}])", oldPassword, newPassword);
        Subject subject = SecurityUtils.getSubject();
        UserInfo userInfo = (UserInfo)subject.getSession().getAttribute(Constant.currentUserKey);
        int id = userInfo.getId();
        User user = userDao.get(User.class, id);
        if (user != null) {
            if (user.getPassword().equals(oldPassword)) {
                user.setPassword(newPassword);
                userDao.save(user);
                userInfo = TransformUtil.fromUser(user);
                subject.getSession().setAttribute(Constant.currentUserKey, userInfo);
                return true;
            } else {
                logger.info("原密码错误");
                return false;
            }
        }
        logger.warn("session中存放的用户信息与数据库中的不符合，请排查原因");
        return false;
    }

    @Override
    public User findById(int id) {
        //TODO 参数校验
        logger.info("findById:[{}]", id);
        return userDao.get(User.class, id);
    }



    @Override
    public User findByAccount(String account) {
        //TODO 参数校验
        logger.info("findByAccount:[{}]", account);
        return userDao.findByAccount(account);
    }

    @Override
    public User findByPhone(String phone) {
        //TODO 参数校验
        logger.info("findMemberByPhone:[{}]", phone);
        return userDao.findByPhone(phone);
    }
}
